// $codepro.audit.disable numericLiterals, variableShouldBeFinal
/* Copyright (c) 2011 Team A CS2340. All rights reserved.*/

package edu.gatech.teama.doctorsoffice.client.view;

import java.awt.Color;

import java.text.SimpleDateFormat;

import java.util.List;

import javax.swing.JPanel;
import javax.swing.JLabel;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;



import org.jfree.chart.plot.XYPlot;

import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;





import edu.gatech.teama.doctorsoffice.client.presenter.MedicalHistoryPresenter;
import edu.gatech.teama.doctorsoffice.model.Appointment;
import edu.gatech.teama.doctorsoffice.model.Patient;


import java.awt.BorderLayout;

/**
 * MedicalHistoryReportPanel.java
 * @author unknown
 * @version 1.0
 */
public class MedicalHistoryReportPanel extends JPanel {

	/**
	 * // $codepro.audit.disable unnecessaryDefaultConstructor
	 * Create the panel.
	 *
	 * @param patient the patient
	 */
	public MedicalHistoryReportPanel(Patient patient) { // $codepro.audit.disable com.instantiations.assist.eclipse.analysis.audit.rule.effectivejava.constructorsOnlyInvokeFinalMethods
		MedicalHistoryPresenter presenter = new MedicalHistoryPresenter(patient);
		setLayout(new BorderLayout(0, 0));
		
		JLabel label = new JLabel("<<Patient Name>>");
		add(label);
		
		JLabel lblMedicalHistory = new JLabel("Medical History");
		add(lblMedicalHistory);
		
		//final XYSeries weight=new XYSeries("Weight");
		final TimeSeries weight = new TimeSeries("Weight");
		final List<Appointment> list = presenter.getAppointments();
		for (Appointment t : list){
			//int[] n=t.getDate().getValues();
			//double date=t.getDate().getYear() * 10000 + 
			//t.getDate().getMonthOfYear() * 100 + t.getDate().getDayOfMonth();
			try {
			weight.add(new Day(t.getDate().getDayOfMonth(), 
					t.getDate().getMonthOfYear(), t.getDate().getYear()),
					t.getTreatmentRecord().getVitalSigns().getWeightLbs());
			} catch (org.jfree.data.general.SeriesException e) {
				System.out.println(e);
			}
			//weight.add(date,t.getTreatmentRecord().getVitalSigns().getWeightLbs());
		}
		//final XYSeriesCollection set=new XYSeriesCollection();
		final TimeSeriesCollection set = new TimeSeriesCollection();
		set.addSeries(weight);
		
		JFreeChart chart = ChartFactory.createTimeSeriesChart(
				"Medical History", "Date", "Weight", set, true, true, false);
		chart.setBackgroundPaint(Color.white);
		final XYPlot plot = chart.getXYPlot();
		plot.setBackgroundPaint(Color.lightGray);
		plot.setDomainGridlinePaint(Color.white);
		plot.setRangeGridlinePaint(Color.white);
		
		final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
		renderer.setSeriesLinesVisible(0, true);
		renderer.setSeriesShapesVisible(1, false);
		plot.setRenderer(renderer);
		
		//final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
		//rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

		final DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("MMM-dd-yyyy"));
		
		ChartPanel panel = new ChartPanel(chart);
		//frame.setBounds(10, 55, 220, 220);
		add(panel);
		//frame.setVisible(true);
	}
}
